package com.elb.sell.controller;

import com.elb.sell.common.controller.BaseController;
import com.elb.sell.common.vo.ResultVoUtil;
import com.elb.sell.pojo.ProductCategory;
import com.elb.sell.pojo.ProductInfo;
import com.elb.sell.service.ProductCategoryService;
import com.elb.sell.service.ProductInfoService;
import com.elb.sell.vo.ProductInfoVO;
import com.elb.sell.vo.ProductVO;
import com.elb.sell.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author HanLiBin
 * @version V1.0
 */
@RestController
@RequestMapping("/buyer/product/")
public class BuyProductController extends BaseController {
    @Autowired
    private ProductCategoryService productCategoryService;
    @Autowired
    private ProductInfoService productInfoService;
    @GetMapping("list")
    public ResultVO list(){
       //获取所有的上架商品
        List<ProductInfo> productInfos=productInfoService.findByProductStatus(0);
        //获取所有上架商品的类目编号
        List<Integer> categories=new ArrayList<>();
        for (ProductInfo productInfo:productInfos){
            if (!categories.contains(productInfo.getCategoryType())){
                categories.add(productInfo.getCategoryType());
            }
        }
        //获取所有上架商品的类目的类目
        List<ProductCategory> productCategories=productCategoryService.findByCategoryTypeIn(categories);
        //拼接数据
        List<ProductVO> productVOS=new ArrayList<>();
        for (ProductCategory productCategory:productCategories){
            List<ProductInfoVO> infos=new ArrayList<>();
            ProductVO productVO=new ProductVO();
            productVO.setCategoryType(productCategory.getCategoryType());
            productVO.setCategoryName(productCategory.getCategoryName());
            for(ProductInfo info:productInfos){
                if (info.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVO productInfoVO=new ProductInfoVO();
                    BeanUtils.copyProperties(info,productInfoVO);
                    infos.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(infos);
            productVOS.add(productVO);
        }
        return ResultVoUtil.success(productVOS);
    }
}
